SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO



alter  function f_warnattermWorkContract
(
  @currfilid int,
  @warningdays int,
  @warndate datetime 
)
returns @resultbill table
(
  choice    bit,
  em_employeeid int,
  em_code    nvarchar(40),
  em_name    nvarchar(60),
  filname    nvarchar(60),
  deptname   nvarchar(60),
  em_sworkcontract datetime,
  em_eworkcontract datetime,
  warndays  int,
  filid     int
)
as
begin

  insert into @resultbill
  select cast(0 as bit) as choice,e.em_employeeid, e.em_code, e.em_name,
     f.fi_filnm, d.ed_deptnm,
     e.em_sworkcontract,e.em_eworkcontract,datediff(day,em_eworkcontract,@warndate) as warndays,e.filid
     from td_empmaster e 
     left join td_filiale f on e.filid=f.fi_filid
     left join td_dept d on e.deptid=d.ed_deptid
     where (e.filid=@currfilid or @currfilid=-99) and em_eWorkContract is not null 
       and em_eWorkContract+@warningdays>=@warndate
       and em_eWorkContract<@warndate
  return 
end

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

